<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h2>1. 链接</h2>
    <code>
      const ws = new WebSocket('ws://124.221.74.149:8003/koa/ws?user_id=?&server_id=?')
    </code>
    <h2>2. 链接 open</h2>
    <code>
      ws.onopen = function (event) { }
    </code>
    <h2>3. 获取默认的聊天记录（需要open之后才可以调用）</h2>

    <code>
      ws.send(JSON.stringify({ type: 'get_history', data: { user_id: 1, server_id: 1 } }))
    </code>

    <h2>4. 发送信息</h2>
    <code>
      ws.send(JSON.stringify({ type: 'addMessage', data: { user_id: 1, server_id: 1, message: "信息" } }))
    </code>

    <h2>5. 相映</h2>
    <code>
      ws.onmessage = function (event) { 
        const { type, data } = JSON.parse(event.data)
        if (type === 'addMessage') {
          console.log(data)
        }
        if(type === "getChatList") {
          console.log(data)
        }
      }
    </code>
  </body>
</html>
